#include "gkt_debug.h"
#include "gkt_board.h"
#include "gkt_wakeup.h"
#include "gkt_fps.h"
#include "gkt_bg_job.h"
#include "gkt_os.h"
#include "gkt_window.h"

#if defined(GKT_CONFIG_FPS_ENABLE) && GKT_CONFIG_FPS_ENABLE
int	fps_capture_retval_global = 0xff;

static void fps_callback(uint32_t status)
{
	gkt_event_s event;
	int retval;

	gkt_trace("window_fps: status = 0x%x\n", status);
	if (GKT_FPS_ST_FINGER_PRESS == status) {
		event.window_id = GKT_WINDOW_ID_ACTIVE;
		event.flags = 0;
		event.major_type = GKT_EVENT_FPS;
		event.minor_type = GKT_FPS_FINGER_PRESS;
		retval = gkt_event_put(&event, GKT_IMMEDIATELY);
		if (__UNLIKELY(retval != GKT_SUCCESS))
			gkt_error("window_fps: put event failed %d\n", retval);
	}
//	gkt_semaphore_release(irq_semaphore_t);
}

static void fps_capture(void)
{
//	int os_status;

	gkt_info("fps_capture: fps_capture ...\n");
	fps_capture_retval_global = gkt_fpsensor_capture_image();
}

int gkt_fps_init(void)
{
	gkt_fpsensor_config_attr_s attr;
	uint32_t wkup_state;
	int retval;

	attr.callback = fps_callback;
	retval = gkt_fpsensor_config(&attr);
	if (retval != GKT_SUCCESS) {
		gkt_error("fps_init: config sensor failed %d\n", retval);
		return retval;
	}

	wkup_state = gkt_fpsensor_get_wakeup_state();
	if (GKT_FPS_WKUP_ST_CERTAIN == wkup_state) {
			gkt_wakeup_set_source(GKT_WAKEUP_SRC_fpsensor);
			gkt_bg_job_register(GKT_BG_JOB_ID(fps_capture), fps_capture);
			gkt_bg_job_start(GKT_BG_JOB_ID(fps_capture), 
				GKT_BG_JOB_PRIORITY_HIGH);
	}

	return GKT_SUCCESS;
}

void gkt_fps_mask_release(void)
{
	//gkt_fpsensor_detect();
}

void gkt_fps_mask(int masked, uint32_t flags)
{
	gkt_unused(flags);

	if (masked)
		gkt_fpsensor_control(GKT_FPS_CTRL_DISABLE, flags);
	else
		gkt_fpsensor_control(GKT_FPS_CTRL_ENABLE, flags);
}

#else

#define gkt_fps_init	NULL
#define gkt_fps_mask	NULL

#endif

#define gkt_fps_event_handler	NULL

GKT_EVENT_DESC_DEFINE(FPS, fps);


